package com.my.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.my.drinkinggame.Main;
import com.my.drinkinggame.Renderer;
import com.my.drinkinggame.StaticSprites;
import com.my.drinkinggame.StaticTextures;

public class MainMenu extends Screen {
	Main main;
	public OrthographicCamera cam;
	Vector3 translationVector;
	public Sprite activeStart;
	public Sprite activeCreate;
	public Sprite activeAC;
	public Sprite activeContinue;
	public Sprite activeRules;
	public float xPos;
	public float yPos;
	public float timer;
	public Sprite bg;
	public boolean resetButtons, gameStarted;
	public MainMenu(Main m) {
		cam = new OrthographicCamera();
		translationVector = new Vector3();
		main = m;
		activeStart = StaticSprites.startBut;
		activeCreate = StaticSprites.createCard;
		activeAC = StaticSprites.addedCardsBut;
		activeContinue = StaticSprites.continueBut;
		activeRules = StaticSprites.rulesBut;
		resetButtons = false;
		gameStarted = false;
		xPos = 0;
		yPos = 0;
		timer = 0;
	}

	public void enter() {
		Gdx.input.setCatchBackKey(true);
		Gdx.input.setInputProcessor(this);
		activeCreate = StaticSprites.createCard;
		activeStart = StaticSprites.startBut;
	}

	@Override
	public boolean keyDown(int arg0) {
		if (Input.Keys.BACK == arg0) {
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int arg2, int arg3) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		xPos = translationVector.x;
		yPos = translationVector.y;
		resetButtons = false;
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int arg2) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		xPos = translationVector.x;
		yPos = translationVector.y;
		return false;
	}

	public void updateContinueBut(){
		if(activeContinue.getBoundingRectangle().contains(xPos, yPos)){
			activeContinue = StaticSprites.continueButSel;
		}
		else{
			activeContinue = StaticSprites.continueBut;
		}
	}

	public void updateStartBut(){
		if(activeStart.getBoundingRectangle().contains(xPos, yPos)){
			activeStart = StaticSprites.startButSel;
		}
		else{
			activeStart = StaticSprites.startBut;
		}
	}

	public void updateCreateBut(){
		if(activeCreate.getBoundingRectangle().contains(xPos, yPos)){
			activeCreate = StaticSprites.createCardSel;
		}
		else{
			activeCreate = StaticSprites.createCard;
		}
	}

	public void updateACBut(){
		if(activeAC.getBoundingRectangle().contains(xPos, yPos)){
			activeAC = StaticSprites.addedCardsButSel;
		}
		else{
			activeAC = StaticSprites.addedCardsBut;
		}
	}

	public void updateRulesBut(){
		if(activeRules.getBoundingRectangle().contains(xPos, yPos)){
			activeRules = StaticSprites.rulesButSel;
		}
		else{
			activeRules = StaticSprites.rulesBut;
		}
	}

	public void resetButs(){
		activeCreate = StaticSprites.createCard;
		activeStart = StaticSprites.startBut;
		activeAC = StaticSprites.addedCardsBut;
		activeContinue = StaticSprites.continueBut;
		activeRules = StaticSprites.rulesBut;
	}

	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int arg2, int arg3) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		if(activeContinue.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			main.setScreen(Main.SCREEN_GAME);
		}
		if(activeStart.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			main.setScreen(Main.SCREEN_NEWGAME);
		}
		if(activeCreate.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			main.setScreen(Main.SCREEN_CREATECARD);
		}
		if(activeAC.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			main.setScreen(Main.SCREEN_ADDEDCARDS);
		}
		if(activeRules.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			main.setScreen(Main.SCREEN_RULES);
		}
		resetButtons = true;
		return false;
	}

	@Override
	public void update() {
		updateStartBut();
		updateCreateBut();
		updateACBut();
		updateContinueBut();
		updateRulesBut();
		if(resetButtons){
			resetButs();
		}
	}


	@Override
	public void render() {
		Renderer.getInstance().renderMainMenu(this);
	}

	public void renderMainMenu(SpriteBatch sb) {
		StaticSprites.mainMenuBG.draw(sb);
		if(!gameStarted){
			activeStart.setPosition(cam.position.x-StaticSprites.startBut.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f);
			activeStart.draw(sb);
			activeCreate.setPosition(cam.position.x-StaticSprites.createCard.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f-StaticSprites.createCard.getHeight()*1.5f);
			activeCreate.draw(sb);
			activeAC.setPosition(cam.position.x-StaticSprites.addedCardsBut.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f-StaticSprites.createCard.getHeight()*3f);
			activeAC.draw(sb);
			activeRules.setPosition(cam.position.x-StaticSprites.rulesBut.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f-StaticSprites.rulesBut.getHeight()*4.5f);
			activeRules.draw(sb);
		}
		else{
			activeContinue.setPosition(cam.position.x-StaticSprites.continueBut.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f);
			activeContinue.draw(sb);
			activeStart.setPosition(cam.position.x-StaticSprites.startBut.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f-StaticSprites.startBut.getHeight()*1.5f);
			activeStart.draw(sb);
			activeCreate.setPosition(cam.position.x-StaticSprites.createCard.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f-StaticSprites.createCard.getHeight()*3f);
			activeCreate.draw(sb);
			activeAC.setPosition(cam.position.x-StaticSprites.addedCardsBut.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f-StaticSprites.addedCardsBut.getHeight()*4.5f);
			activeAC.draw(sb);
			activeRules.setPosition(cam.position.x-StaticSprites.rulesBut.getWidth()/2,cam.position.y+Gdx.graphics.getHeight()/4.5f-StaticSprites.rulesBut.getHeight()*6f);
			activeRules.draw(sb);
		}
	}

	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}
}
